"""
YAML配置文件操作工具模块。
"""

import yaml
from typing import Dict, Any

def update_nested_dict(d: Dict[str, Any], u: Dict[str, Any]) -> Dict[str, Any]:
    """
    递归更新嵌套字典。
    
    Args:
        d: 原始字典
        u: 更新字典
        
    Returns:
        更新后的字典
    """
    for k, v in u.items():
        if isinstance(v, dict):
            d[k] = update_nested_dict(d.get(k, {}), v)
        else:
            d[k] = v
    return d

def update_and_write_to_file(file_path: str, update_dict: Dict[str, Any]) -> None:
    """
    更新YAML文件中的配置。
    
    Args:
        file_path: YAML文件路径
        update_dict: 要更新的配置字典
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            config = yaml.safe_load(f) or {}
    except FileNotFoundError:
        config = {}
        
    config = update_nested_dict(config, update_dict)
    
    with open(file_path, 'w', encoding='utf-8') as f:
        yaml.safe_dump(config, f, default_flow_style=False, allow_unicode=True) 